home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / sbin / on_ac_power < prev    next >
Encoding:
Text File  |  2006-08-21  |  1.7 KB  |  69 lines

  1. #!/bin/sh
  2. #
  3. # Returns 0 (true) if on AC power
  4. #         1 (false) if not on AC power
  5. #         255 (false) if can't tell
  6. #
  7. # Example shell script:
  8. #     if on_ac_power; then
  9. #       echo We're on AC power
  10. #     else
  11. #       echo Can't say we're on AC power
  12. #     fi
  13.  
  14. set -e
  15.  
  16. # ACPI
  17. #
  18. # This algorithm is complicated by the possibility of multiple AC
  19. # adapters.  We scan the ac_adapter directory looking for adapters
  20. # that have known states.  If any adapter is on-line, we return 0.  If
  21. # no adapters are on-line but one or more are off-line, we return 1.
  22. #
  23. if /sbin/acpi_available && [ -d /proc/acpi/ac_adapter ]; then
  24.     OFF_LINE_P=no
  25.     for FN in /proc/acpi/ac_adapter/*; do
  26.     if [ -d "${FN}" ]; then
  27.         if [ -r "${FN}/state" ]; then
  28.         grep --quiet on-line "${FN}/state" && exit 0
  29.         grep --quiet off-line "${FN}/state" && OFF_LINE_P=yes
  30.         elif [ -r "${FN}/status" ]; then
  31.         grep --quiet on-line "${FN}/status" && exit 0
  32.         grep --quiet off-line "${FN}/status" && OFF_LINE_P=yes
  33.         fi
  34.     fi
  35.     done
  36.     [ "${OFF_LINE_P}" = "yes" ] && exit 1
  37. fi
  38.  
  39. # PMU
  40. if [ -r /proc/pmu/info ]; then
  41.     exec awk </proc/pmu/info '
  42.     BEGIN { FS=":"; ret = 255 }
  43.     /^AC Power.*1$/ { ret = 0; exit }
  44.     /^AC Power.*0$/ { ac = 1 }
  45.         /^Battery.*/ {
  46.                 if ($2 ~/0/ && ac == 1)
  47.                         ret = 0
  48.                 else
  49.                         ret = 1
  50.                 exit }
  51.     END { exit ret }
  52.     '
  53. fi
  54.  
  55. # APM
  56. if [ -r /proc/apm ] && /sbin/apm_available; then
  57.     exec awk </proc/apm '
  58.     BEGIN { ret = 255 }
  59.     /^[0-9.a-zA-Z]* [0-9.]* 0x.. 0x../ {
  60.         if ($4 == "0x01") { ret = 0; exit }
  61.         else if ($4 == "0x00") { ret = 1; exit }
  62.     }
  63.     END { exit ret }
  64.     '
  65. fi
  66.  
  67. # nothing is available
  68. exit 255
  69.